package io.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhou.common.entity.PmsCategory;
import io.zhou.dao.CategoryDAO;
import io.zhou.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceimpl extends ServiceImpl<CategoryDAO, PmsCategory> implements CategoryService {
    @Override
    public List<PmsCategory> tree() {
        //查询所有一级分类
        QueryWrapper<PmsCategory> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_cid",0);
        List<PmsCategory> list = baseMapper.selectList(wrapper);
//        System.out.println("---------------------1");
        //遍历一级分类
        for (PmsCategory pmsCategory : list) {
            //通过一级分类查询二级分类
//            Long catId = pmsCategory.getCatId();
            QueryWrapper<PmsCategory> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_cid",pmsCategory.getCatId());
            List<PmsCategory> list1 = baseMapper.selectList(wrapper1);
            //把查询到二级分类赋值给一级分类
            pmsCategory.setChildrenn(list1);
            //遍历二级分类查询三级分类
//            System.out.println("----------------------------2");
            for (PmsCategory category : list1) {
                QueryWrapper<PmsCategory> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("parent_cid",category.getCatId());
                List<PmsCategory> list2 = baseMapper.selectList(wrapper2);
                category.setChildrenn(list2);
//                System.out.println("0---------------------------3");
            }


        }
        return list;
    }
}
